home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / STUTTGART / LANG / ADA / GNAT / !gcc / adainc / 6 / ads / s-fileio < prev    next >
Text File  |  1996-02-12  |  12KB  |  250 lines

  1. ------------------------------------------------------------------------------
  2. --                                                                          --
  3. --                         GNAT RUN-TIME COMPONENTS                         --
  4. --                                                                          --
  5. --                       S Y S T E M . F I L E _ I O                        --
  6. --                                                                          --
  7. --                                 S p e c                                  --
  8. --                                                                          --
  9. --                            $Revision: 1.14 $                             --
  10. --                                                                          --
  11. --     Copyright (C) 1992,1993,1994,1995 Free Software Foundation, Inc.     --
  12. --                                                                          --
  13. -- GNAT is free software;  you can  redistribute it  and/or modify it under --
  14. -- terms of the  GNU General Public License as published  by the Free Soft- --
  15. -- ware  Foundation;  either version 2,  or (at your option) any later ver- --
  16. -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
  17. -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
  18. -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
  19. -- for  more details.  You should have  received  a copy of the GNU General --
  20. -- Public License  distributed with GNAT;  see file COPYING.  If not, write --
  21. -- to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, --
  22. -- MA 02111-1307, USA.                                                      --
  23. --                                                                          --
  24. -- As a special exception,  if other files  instantiate  generics from this --
  25. -- unit, or you link  this unit with other files  to produce an executable, --
  26. -- this  unit  does not  by itself cause  the resulting  executable  to  be --
  27. -- covered  by the  GNU  General  Public  License.  This exception does not --
  28. -- however invalidate  any other reasons why  the executable file  might be --
  29. -- covered by the  GNU Public License.                                      --
  30. --                                                                          --
  31. -- GNAT was originally developed  by the GNAT team at  New York University. --
  32. -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
  33. --                                                                          --
  34. ------------------------------------------------------------------------------
  35.  
  36. --  This package provides support for the routines described in (RM A.8.2)
  37. --  which are common to Text_IO, Direct_IO, Sequential_IO and Stream_IO.
  38.  
  39. with Ada.Streams;
  40. with Interfaces.C_Streams;
  41.  
  42. with System.File_Control_Block;
  43.  
  44. package System.File_IO is
  45.  
  46.    package FCB renames System.File_Control_Block;
  47.    package ICS renames Interfaces.C_Streams;
  48.  
  49.    ---------------------
  50.    -- File Management --
  51.    ---------------------
  52.  
  53.    procedure Open
  54.      (File_Ptr  : in out FCB.AFCB_Ptr;
  55.       Dummy_FCB : in out FCB.AFCB'Class;
  56.       Mode      : FCB.File_Mode;
  57.       Name      : String;
  58.       Form      : String;
  59.       Amethod   : Character;
  60.       Creat     : Boolean;
  61.       Text      : Boolean;
  62.       C_Stream  : ICS.FILEs := ICS.NULL_Stream);
  63.    --  This routine is used for both Open and Create calls:
  64.    --
  65.    --    File_Ptr is the file type, which must be null on entry
  66.    --    (i.e. the file must be closed before the call).
  67.    --
  68.    --    Dummy_FCB is a default initialized file control block of appropriate
  69.    --    type. Note that the tag of this record indicates the type and length
  70.    --    of the control block. This control block is used only for the purpose
  71.    --    of providing the controlling argument for calling the write version
  72.    --    of Allocate_AFCB. It has no other purpose, and its fields are never
  73.    --    read or written.
  74.    --
  75.    --    Mode is the required mode
  76.    --
  77.    --    Name is the file name, with a null string indicating that a temporary
  78.    --    file is to be created (only permitted in create mode, not open mode)
  79.    --
  80.    --    Creat is True for a create call, and false for an open call
  81.    --
  82.    --    Text is set True to open the file in text mode (w+t or r+t) instead
  83.    --    of the usual binary mode open (w+b or r+b).
  84.    --
  85.    --    Form is the form string given in the open or create call, this is
  86.    --    stored in the AFCB, but otherwise is not used by this or any other
  87.    --    routine in this unit (except Form which retrieves the original value)
  88.    --
  89.    --    Amethod indicates the access method
  90.    --
  91.    --      D = Direct_IO
  92.    --      Q = Sequential_IO
  93.    --      S = Stream_IO
  94.    --      T = Text_IO
  95.    --      W = Wide_Text_IO
  96.    --
  97.    --    C_Stream is left at its default value for the normal case of an
  98.    --    Open or Create call as defined in the RM. The only time this is
  99.    --    non-null is for the Open call from Ada.xxx_IO.C_Streams.Open.
  100.    --
  101.    --  On return, if the open/create succeeds, then the fields of File are
  102.    --  filled in, and this value is copied to the heap. File_Ptr points to
  103.    --  this allocated file control block. If the open/create fails, then the
  104.    --  fields of File are undefined, and File_Ptr is unchanged.
  105.  
  106.    procedure Close (File : in out FCB.AFCB_Ptr);
  107.    --  The file is closed, all storage associated with it is released, and
  108.    --  File is set to null. Note that this routine calls AFCB_Close to perform
  109.    --  any specialized close actions, then closes the file at the system level,
  110.    --  then frees the mode and form strings, and finally calls AFCB_Free to
  111.    --  free the file control block itself, setting File to null.
  112.  
  113.    procedure Delete (File : in out FCB.AFCB_Ptr);
  114.    --  The indicated file is unlinked
  115.  
  116.    procedure Reset (File : in out FCB.AFCB_Ptr; Mode : in FCB.File_Mode);
  117.    --  The file is reset, and the mode changed as indicated.
  118.  
  119.    procedure Reset (File : in out FCB.AFCB_Ptr);
  120.    --  The files is reset, and the mode is unchanged
  121.  
  122.    function Mode (File : in FCB.AFCB_Ptr) return FCB.File_Mode;
  123.    --  Returns the mode as supplied by create, open or reset
  124.  
  125.    function Name (File : in FCB.AFCB_Ptr) return String;
  126.    --  Returns the file name as supplied by Open or Create. Raises Use_Error
  127.    --  if used with temporary files or standard files.
  128.  
  129.    function Form (File : in FCB.AFCB_Ptr) return String;
  130.    --  Returns the form as supplied by create, open or reset
  131.    --  The string is normalized to all lower case letters.
  132.  
  133.    function Is_Open (File : in FCB.AFCB_Ptr) return Boolean;
  134.    --  Determines if file is open or not
  135.  
  136.    ----------------------
  137.    -- Utility Routines --
  138.    ----------------------
  139.  
  140.    --  Some internal routines not defined in A.8.2. These are routines which
  141.    --  provide required common functionality shared by separate packages.
  142.  
  143.    procedure Chain_File (File : FCB.AFCB_Ptr);
  144.    --  Used to chain the given file into the list of open files. Normally this
  145.    --  is done implicitly by Open. Chain_File is used for the spcial cases of
  146.    --  the system files defined by Text_IO (stdin, stdout, stderr) which are
  147.    --  not opened in the normal manner. Note that the caller is responsible
  148.    --  for task lock out to protect the global data structures if this is
  149.    --  necessary (it is needed for the calls from within this unit itself,
  150.    --  but not required for the calls from Text_IO and Wide_Text_IO that
  151.    --  are made during elaboration of the environment task).
  152.  
  153.    procedure Check_File_Open (File : FCB.AFCB_Ptr);
  154.    --  If the current file is not open, then Status_Error is raised.
  155.    --  Otherwise control returns normally (with File pointing to the
  156.    --  control block for the open file.
  157.  
  158.    procedure Check_Read_Status (File : FCB.AFCB_Ptr);
  159.    --  If the current file is not open, then Status_Error is raised. If
  160.    --  the file is open, then the mode is checked to ensure that reading
  161.    --  is permitted, and if not Mode_Error is raised, otherwise control
  162.    --  returns normally.
  163.  
  164.    procedure Check_Write_Status (File : FCB.AFCB_Ptr);
  165.    --  If the current file is not open, then Status_Error is raised. If
  166.    --  the file is open, then the mode is checked to ensure that writing
  167.    --  is permitted, and if not Mode_Error is raised, otherwise control
  168.    --  returns normally.
  169.  
  170.    function End_Of_File (File : FCB.AFCB_Ptr) return Boolean;
  171.    --  File must be opened in read mode. True is returned if the stream is
  172.    --  currently positioned at the end of file, otherwise False is returned.
  173.    --  The position of the stream is not affected.
  174.  
  175.    procedure Flush (File : FCB.AFCB_Ptr);
  176.    --  Flushes the stream associated with the given file. The file must be
  177.    --  open and in write mode (if not, an appropriate exception is raised)
  178.  
  179.    function Form_Boolean
  180.      (Form    : String;
  181.       Keyword : String;
  182.       Default : Boolean)
  183.       return    Boolean;
  184.    --  Searches form string for an entry of the form Keyword=xx where xx is
  185.    --  either Yes/No or y/n. Returns True if Yes or Y is found, False if No
  186.    --  or N is found. If the keyword parameter is not found, returns the
  187.    --  value given as Default. May raise Use_Error if a form string syntax
  188.    --  error is detected. Keyword and Form must be in lower case.
  189.  
  190.    function Form_Integer
  191.      (Form    : String;
  192.       Keyword : String;
  193.       Default : Integer)
  194.       return    Integer;
  195.    --  Searches form string for an entry of the form Keyword=xx where xx is
  196.    --  an unsigned decimal integer in the range 0 to 999_999. Returns this
  197.    --  integer value if it is found. If the keyword parameter is not found,
  198.    --  returns the value given as Default. Raise Use_Error if a form string
  199.    --  syntax error is detected. Keyword and Form must be in lower case.
  200.  
  201.    procedure Form_Parameter
  202.      (Form    : String;
  203.       Keyword : String;
  204.       Start   : out Natural;
  205.       Stop    : out Natural);
  206.    --  Searches form string for an entry of the form Keyword=xx and if found
  207.    --  Sets Start and Stop to the first and last characters of xx. Keyword
  208.    --  and Form must be in lower case. If no entry matches, then Start is
  209.    --  set to zero. Use_Error can be raised if a malformed string is found,
  210.    --  but there is no guarantee of full syntax checking.
  211.  
  212.    procedure Read_Buf
  213.      (File : FCB.AFCB_Ptr;
  214.       Buf  : Address;
  215.       Siz  : Interfaces.C_Streams.size_t);
  216.    --  Reads Siz bytes from File.Stream into Buf. The caller has checked
  217.    --  that the file is open in read mode. Raises an exception if Siz bytes
  218.    --  cannot be read (End_Error if no data was read, Data_Error if a partial
  219.    --  buffer was read, Device_Error if an error occurs).
  220.  
  221.    procedure Read_Buf
  222.      (File  : FCB.AFCB_Ptr;
  223.       Buf   : Address;
  224.       Siz   : in Interfaces.C_Streams.size_t;
  225.       Count : out Interfaces.C_Streams.size_t);
  226.    --  Reads Siz bytes from File.Stream into Buf. The caller has checked
  227.    --  that the file is open in read mode. Device Error is raised if an error
  228.    --  occurs. Count is the actual number of bytes read, which may be less
  229.    --  than Siz if the end of file is encountered.
  230.  
  231.    procedure Append_Set (File : FCB.AFCB_Ptr);
  232.    --  If the mode of the file is Append_File, then the file is positioned
  233.    --  at the end of file using fseek, otherwise this call has no effect.
  234.  
  235.    procedure Write_Buf
  236.      (File : FCB.AFCB_Ptr;
  237.       Buf  : Address;
  238.       Siz  : Interfaces.C_Streams.size_t);
  239.    --  Writes size_t bytes to File.Stream from Buf. The caller has checked
  240.    --  that the file is open in write mode. Raises Device_Error if the
  241.    --  complete buffer cannot be written.
  242.  
  243. private
  244.    pragma Inline (Check_Read_Status);
  245.    pragma Inline (Check_Write_Status);
  246.    pragma Inline (Form);
  247.    pragma Inline (Mode);
  248.  
  249. end System.File_IO;
  250.